package cs340.stormtroopers.junitTests.phase4WhiteBox;

import java.util.ArrayList;
import java.util.HashSet;

import junit.framework.TestCase;
import cs340.stormtroopers.hypeerweb.HyPeerWebBuilder;
import cs340.stormtroopers.hypeerweb.Node;
import cs340.stormtroopers.hypeerweb.SimplifiedNodeDomain;

/**
 * White box tests for the HyPeerWebBuilder.
 * Includes loop and data flow tests.
 * @author Andy Morrow
 *
 */
public class HyPeerWebBuilderTest extends TestCase
{
	public void testLoops()
	{
		// first test: zero runs through the loops
		ArrayList<SimplifiedNodeDomain> nodeDomains = new ArrayList<SimplifiedNodeDomain>();
		HyPeerWebBuilder builder = new HyPeerWebBuilder(nodeDomains);
		assertTrue(builder.getNodeMap().size() == 0);
		// second test: one run
		HashSet<Integer> blankList = new HashSet<Integer>();
		SimplifiedNodeDomain nodeDomain = new SimplifiedNodeDomain(0, 0, blankList, blankList, blankList, 0, -1, -1, 0);
		nodeDomains.add(nodeDomain);
		builder = new HyPeerWebBuilder(nodeDomains);
		assertTrue(builder.getNodeMap().size() == 1);
		// second test: two runs
		nodeDomain = new SimplifiedNodeDomain(1, 1, blankList, blankList, blankList, 1, -1, -1, 0);
		nodeDomains.add(nodeDomain);
		builder = new HyPeerWebBuilder(nodeDomains);
		assertTrue(builder.getNodeMap().size() == 2);
		// third test: random large number of runs
		// we'll choose 42
		nodeDomains.clear();
		for (int i = 0; i < 42; i++)
		{
			nodeDomains.add(new SimplifiedNodeDomain(i, 10, blankList, blankList, blankList,  i, -1, -1, 0));
		}
		builder = new HyPeerWebBuilder(nodeDomains);
		assertTrue(builder.getNodeMap().size() == 42);
	}
	
	/**
	 * We'll test the flow of data relative to the connections object.
	 * It's not really a variable, but we can treat it as such.
	 */
	public void testDataFlow()
	{
		HashSet<Integer> blankList = new HashSet<Integer>();
		SimplifiedNodeDomain nodeDomain = new SimplifiedNodeDomain(0, 0, blankList, blankList, blankList, -1, -1, -1, 0);
		ArrayList<SimplifiedNodeDomain> nodeDomainList = new ArrayList<SimplifiedNodeDomain>();
		nodeDomainList.add(nodeDomain);
		// try with all the data flow not getting assigned a real node
		HyPeerWebBuilder builder = new HyPeerWebBuilder(nodeDomainList);
		assertTrue(builder.getNodeMap().size() == 1);
		Node built = builder.getNodeMap().entrySet().iterator().next().getValue();
		assertTrue(built.getFold() == Node.NULL_NODE);
		assertTrue(built.getSurrogateFold() == Node.NULL_NODE);
		assertTrue(built.getInverseSurrogateFold() == Node.NULL_NODE);
		// now try with all the data flow being true
		nodeDomain = new SimplifiedNodeDomain(0, 0, blankList, blankList, blankList, 0, 0, 0, 0);
		nodeDomainList.clear();
		nodeDomainList.add(nodeDomain);
		builder = new HyPeerWebBuilder(nodeDomainList);
		assertTrue(builder.getNodeMap().size() == 1);
		built = builder.getNodeMap().entrySet().iterator().next().getValue();
		assertTrue(built.getFold() == built);
		assertTrue(built.getSurrogateFold() == built);
		assertTrue(built.getInverseSurrogateFold() == built);
	}
}
